package org.txazo.music.web.action.json;

import java.io.File;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.txazo.music.service.MusicService;
import org.txazo.music.util.ConstantsUtil;
import org.txazo.music.util.StringUtil;
import org.txazo.music.web.action.base.JsonBaseAction;

/**
 * UploadMusicJsonAction
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 * 
 */
@Controller
@Scope("session")
public class UploadMusicJsonAction extends JsonBaseAction {

	private static final long serialVersionUID = 1L;

	/** 文件 */
	private File upload;
	/** 文件名称 */
	private String uploadFileName;
	/** 文件类型 */
	private String uploadContentType;
	/** 文件流派 */
	private String style;

	/** 提示消息 */
	private String tip;

	/** 存储路径 */
	private static String savePath = null;

	@Autowired
	private MusicService musicService;

	@Action(value = "upload", results = { @Result(name = SUCCESS, type = "json", params = {
			"contentType", "text/html" }) })
	public String upload() {
		if (upload.length() < ConstantsUtil.UPLOAD_MUSIC_MAXSIZE) {
			if (StringUtil.filterTypes(uploadContentType,
					ConstantsUtil.UPLOAD_MUSIC_ALLOWTYPES.split(","))) {
				File file = new File(getSavePath());
				if (!file.exists()) {
					file.mkdir();
				}
			} else {
				tip = "文件类型错误";
			}
		} else {
			tip = "文件过大";
		}

		return SUCCESS;
	}

	public static String getSavePath() {
		if (savePath == null) {
			savePath = ServletActionContext.getServletContext().getRealPath(
					ConstantsUtil.UPLOAD_MUSIC_SAVEPATH);
		}
		return savePath;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getTip() {
		return tip;
	}

}
